package top.codedance.iotp.tools.terminal.ui;

import java.awt.*;
import java.net.URL;

import top.codedance.iotp.common.util.CRC16Util;
import top.codedance.iotp.common.util.HexUtil;
import top.codedance.iotp.tools.terminal.TerminalApplication;

import javax.swing.*;

/**
 *
 * @author xuedianfu
 */
public class ModbusGen extends javax.swing.JDialog {

    private ShellFrame sf;

    /**
     * Creates new form ModbusGen
     */
    public ModbusGen(ShellFrame sf) {
        super(sf, true);
        this.sf = sf;
        initComponents();
        URL url = Thread.currentThread().getContextClassLoader().getResource("logo.gif");
        ImageIcon icon = new ImageIcon(url);
        setIconImage(icon.getImage());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(this.sf);
        setMinimumSize(new Dimension(421, 286));
        setTitle("Modbus命令生成器");
        this.jta_data.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bg_oprationType = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jtf_slaveId = new javax.swing.JTextField();
        jtf_function = new javax.swing.JTextField();
        jtf_address = new javax.swing.JTextField();
        jb_gen = new javax.swing.JButton();
        jcb_crc = new javax.swing.JCheckBox();
        jLabel6 = new javax.swing.JLabel();
        jtf_dataLength = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jta_data = new javax.swing.JTextArea();
        jrb_read = new javax.swing.JRadioButton();
        jrb_write = new javax.swing.JRadioButton();
        jLabel8 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("SlaveId");

        jLabel2.setText("Function");

        jLabel3.setText("Address");

        jLabel4.setText("CRC");

        jLabel5.setText("Modbus生成器");

        jtf_slaveId.setText("01");
        jtf_slaveId.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtf_slaveIdKeyTyped(evt);
            }
        });

        jtf_function.setText("03");
        jtf_function.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtf_functionKeyTyped(evt);
            }
        });

        jtf_address.setText("00");
        jtf_address.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtf_addressKeyTyped(evt);
            }
        });

        jb_gen.setText("生成");
        jb_gen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_genActionPerformed(evt);
            }
        });

        jcb_crc.setSelected(true);
        jcb_crc.setText("CRC校验");

        jLabel6.setText("Data length");

        jtf_dataLength.setText("02");
        jtf_dataLength.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtf_dataLengthKeyTyped(evt);
            }
        });

        jLabel7.setText("Data");

        jta_data.setColumns(20);
        jta_data.setRows(5);
        jta_data.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jta_dataKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(jta_data);

        bg_oprationType.add(jrb_read);
        jrb_read.setSelected(true);
        jrb_read.setText("读取");
        jrb_read.setActionCommand("read");

        bg_oprationType.add(jrb_write);
        jrb_write.setText("写入");
        jrb_write.setActionCommand("write");
        jrb_write.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jrb_writeItemStateChanged(evt);
            }
        });

        jLabel8.setForeground(new java.awt.Color(255, 0, 0));
        jLabel8.setText("注：请使用十六进制数据输入");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel7)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jcb_crc)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jb_gen))
                            .addComponent(jScrollPane1)
                            .addComponent(jtf_slaveId, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
                            .addComponent(jtf_function)
                            .addComponent(jtf_address)
                            .addComponent(jtf_dataLength)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(jrb_read)
                        .addGap(18, 18, 18)
                        .addComponent(jrb_write))
                    .addComponent(jLabel8))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jrb_read)
                    .addComponent(jrb_write))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jtf_slaveId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2))
                    .addComponent(jtf_function, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtf_address, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jtf_dataLength, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jcb_crc)
                        .addComponent(jb_gen)))
                .addGap(18, 18, 18)
                .addComponent(jLabel8)
                .addGap(26, 26, 26))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jb_genActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_genActionPerformed
        StringBuilder sb = new StringBuilder();
        sb.append(this.jtf_slaveId.getText());
        sb.append(this.jtf_function.getText());
        sb.append(this.jtf_address.getText());
        sb.append(this.jtf_dataLength.getText());
        if(this.bg_oprationType.getSelection().getActionCommand().equals("write")){
            sb.append(this.jta_data.getText());
        }
        if(this.jcb_crc.isSelected()){
            sb.append(CRC16Util.getCRC3(HexUtil.toBytes(sb.toString().replaceAll("\\s+", ""))));
        }
        sf.appendCmd(sb.toString().replaceAll("\\s+", "").toUpperCase());
        this.setVisible(false);
    }//GEN-LAST:event_jb_genActionPerformed

    private void jrb_writeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jrb_writeItemStateChanged
        if(this.bg_oprationType.getSelection().getActionCommand().equals("write")){
            this.jta_data.setEnabled(true);
        }else{
            this.jta_data.setEnabled(false);
        }
    }//GEN-LAST:event_jrb_writeItemStateChanged

    private void jtf_slaveIdKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtf_slaveIdKeyTyped
        JTextField jtf = (JTextField)evt.getSource();
        if (!TerminalApplication.hexNumbers.contains(evt.getKeyChar())) {
            evt.consume();
        }
    }//GEN-LAST:event_jtf_slaveIdKeyTyped

    private void jtf_functionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtf_functionKeyTyped
        jtf_slaveIdKeyTyped(evt);
    }//GEN-LAST:event_jtf_functionKeyTyped

    private void jtf_addressKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtf_addressKeyTyped
        jtf_slaveIdKeyTyped(evt);
    }//GEN-LAST:event_jtf_addressKeyTyped

    private void jtf_dataLengthKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtf_dataLengthKeyTyped
        jtf_slaveIdKeyTyped(evt);
    }//GEN-LAST:event_jtf_dataLengthKeyTyped

    private void jta_dataKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jta_dataKeyTyped
        jtf_slaveIdKeyTyped(evt);
    }//GEN-LAST:event_jta_dataKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup bg_oprationType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jb_gen;
    private javax.swing.JCheckBox jcb_crc;
    private javax.swing.JRadioButton jrb_read;
    private javax.swing.JRadioButton jrb_write;
    private javax.swing.JTextArea jta_data;
    private javax.swing.JTextField jtf_address;
    private javax.swing.JTextField jtf_dataLength;
    private javax.swing.JTextField jtf_function;
    private javax.swing.JTextField jtf_slaveId;
    // End of variables declaration//GEN-END:variables
}
